import React, { Component } from 'react'
import { Table } from "antd"
import { getScoreByExamId } from "@/services/exam"

import { connect } from "dva"
import { getCookie } from '@/utils/cookie/cookie'
import { getStudentByExamId } from '@/services/exam'

const statusMap = {
    "normal": "正常交卷",
    "overtime": "超时交卷"
}
const statusEnum = {
    "normal": 1,
    "overtime": 2
}
const columns = [
    {
        dataIndex: 'index',
        title: '序号',
        render: (_, __, index) => {
            return index + 1
        }
    },
    {
        dataIndex: 'stu_id',
        title: '学号',
    },
    {
        dataIndex: 'stu_name',
        title: '姓名',
    },
    {
        dataIndex: 'stu_class',
        title: '班级',
    },
    {
        dataIndex: 'score',
        title: '得分',
        sorter: (a, b) => {
            return a.score - b.score
        }
    },
    {
        dataIndex: 'status',
        title: '状态',
        render: (text) => {
            return statusMap[text] || text || "未交卷"
        }
    }
]
const mapDispatchToProps = (dispatch) => {
    return {
        changeExamListTabTeach(examListTab) {
            dispatch({
                type: `teacher/changeState`,
                result: { data: examListTab },
                field: "examListTab",
            });
        },
        changeToStudentExamScore() {
            dispatch({
                type: `teacher/changeState`,
                result: {
                    data: "studentExamScore"
                },
                field: 'examListTab'
            })
        },
        changeShowScore(configOfShowScore) {
            dispatch({
                type: `teacher/changeShowScore`,
                configOfShowScore
            })
        },
        changeExamListTabStu(examListTab) {
            dispatch({
                type: `student/changeState`,
                result: { data: examListTab },
                field: "examListTab",
            });
        },
    }
}
class Index extends Component {

    constructor(props) {
        super(props)
        this.state = {
            studentScoreList: []
        }
    }

    componentDidMount() {
        if (this.props.info) {
            const { key } = this.props.info;
            (async () => {
                let studentScoreList = []
                const result1 = await getStudentByExamId({ exam_id: key })
                const result2 = await getScoreByExamId({ exam_id: key })
                if (result1.status === 0) {
                    result1.data.forEach((data, index) => {
                        studentScoreList.push({
                            ...data,
                            answer_paper_id: index
                        })
                    })
                }
                if (result2.status === 0) {
                    result2.data.forEach((data) => {
                        const stu_id = data.stu_id
                        const index = studentScoreList.findIndex((data) => {
                            return data.stu_id === stu_id
                        })
                        if (index > -1) {
                            studentScoreList.splice(index, 1, data)
                        }
                    })
                }
                studentScoreList = studentScoreList.map((data) => {
                    return {
                        key: data.stu_id,
                        ...data
                    }
                })
                studentScoreList = studentScoreList.sort((a, b) => {
                    const aNumber = statusEnum[a.status] || 100
                    const bNumber = statusEnum[b.status] || 100
                    return aNumber - bNumber
                })
                this.setState({
                    studentScoreList: studentScoreList
                })
            })();
        } else {
            if (getCookie("identity") === "student") {
                this.props.changeExamListTabStu()
            } else {
                this.props.changeExamListTabTeach()
            }
        }
    }

    render() {
        if (this.props.info) {
            return <div className="StudentScore" style={{
                width: '100%'
            }}>
                <Table
                    style={{
                        width: "100%"
                    }}
                    columns={columns}
                    onRow={(row) => {
                        return {
                            onClick: () => {
                                this.props.changeShowScore({
                                    ...this.props.info,
                                    stu_id: row.stu_id,
                                    stu_name: row.stu_name
                                })
                                this.props.changeToStudentExamScore()
                            }
                        }
                    }}
                    dataSource={this.state.studentScoreList}>
                </Table>
            </div>
        } else {
            if (getCookie("identity") === "student") {
                this.props.changeExamListTabStu()
            } else {
                this.props.changeExamListTabTeach()
            }
            return ""
        }
    }
}
export default connect(null, mapDispatchToProps)(Index)